package com.luoxiang.memberserver;

import java.io.IOException;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;

import com.alibaba.fastjson.JSON;
import com.luoxiang.register.LxState;
import com.luoxiang.register.ZookeeperRegister;

public class MemberServer implements Watcher{

	ZookeeperRegister zookeeperRegister=new ZookeeperRegister();
	private ZooKeeper zooKeeper;
	
	public void register(String serverInfo) {
		try {
			zooKeeper = zookeeperRegister.getConnection(this);
			Thread.sleep(10000l);
			String path = zooKeeper.create(ZookeeperRegister.ROOT+"/server", serverInfo.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
			System.out.println("创建节点成功"+path);
		} catch (KeeperException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void process(WatchedEvent event) {
		// TODO Auto-generated method stub
	}
	
	public static void main(String[] args) throws IOException {
		MemberServer member = new MemberServer();
		LxState state = new LxState();
		state.setClientName(null);
		state.setIp("192.168.2.100");
		state.setPort("80");
		state.setStatus("init");
		state.setServerName("会员系统");
		String serverInfo = JSON.toJSONString(state);
		member.register(serverInfo);
		System.out.println("我是服务端");
	    System.in.read();
	}
	
}
